
package com.wyzz.hr.action;

import java.util.Date;
import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.BaseDownloadAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IEmpTransferBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Emptransfer;
import com.wyzz.hr.pojo.Emptype;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;

public class EmpTransfer extends BaseDownloadAction {

	public EmpTransfer() {
		page = new Pager();
		outputIoName = "OEmpTransfer";
		searchOrExport = null;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String list() throws Exception {
		getDrillDownList();
		transferList = searchTransfer();
		if ("export".equals(searchOrExport))
			return downloadToExcel(transferList, outputIoName, outmatchModelId,
					"transfer");
		else
			return "success";
	}

	private List searchTransfer() {
		DetachedCriteria dcTransfer = searchTransferWithEmp_DC();
		addCriteria(dcTransfer);
		IEmpTransferBo empTransferBo = (IEmpTransferBo) getBean("empTransferBo");
		return empTransferBo.findTransfer(dcTransfer, page, searchOrExport);
	}

	private DetachedCriteria searchTransferWithEmp_DC() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emptransfer.class);
		dc.createAlias("employee", "emp", 1);
		dc.createAlias(Emptransfer.PROP_EFT_OLD_DEPT_NO, "oldDepartment", 1);
		dc.createAlias(Emptransfer.PROP_EFT_NEW_DEPT_NO, "newDepartment", 1);
		dc.createAlias(Emptransfer.PROP_EFT_NEW_PB_NO, "oldPbNo", 1);
		dc.createAlias(Emptransfer.PROP_EFT_OLD_PB_NO, "newPbNo", 1);
		if ("export".equals(searchOrExport)) {
			dc.setFetchMode((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_DEPT_NO).toString(), FetchMode.JOIN);
			dc.setFetchMode((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_PB_NO).toString(), FetchMode.JOIN);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_LOCATION_NO).toString(), "location", 1);
			dc.setFetchMode((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_TYPE).toString(), FetchMode.JOIN);
			dc.setFetchMode((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_BENEFIT_TYPE).toString(), FetchMode.JOIN);
		}
		return dc;
	}

	public void addCriteria(DetachedCriteria dc) {
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "emp", 1);
		addOrders(dc, page,
				new String[] {
						(new StringBuilder()).append(
								Emptransfer.PROP_EFT_TRANSFER_DATE).append(
								"-down").toString(),
						(new StringBuilder()).append("emp.").append(
								Employee.PROP_EMP_DISTINCT_NO).append("-up")
								.toString() });
		BaseCrit.addEmpDC(dc, "emp", employeeId);
		BaseCrit.addDC(dc, Emptransfer.PROP_EFT_TRANSFER_TYPE, "eq",
				new String[] { eftTransferType });
		BaseCrit.addDeptDC(dc, Emptransfer.PROP_EFT_OLD_DEPT_NO,
				Emptransfer.PROP_EFT_OLD_PB_NO, null, new Department(
						eftOldDeptNo));
		BaseCrit.addDeptDC(dc, Emptransfer.PROP_EFT_NEW_DEPT_NO,
				Emptransfer.PROP_EFT_NEW_PB_NO, null, new Department(
						eftNewDeptNo));
		BaseCrit.addDCDateRange(dc, Emptransfer.PROP_EFT_TRANSFER_DATE,
				eftTransferDate, eftTransferDateTo);
		BaseCrit.addDC(dc, Emptransfer.PROP_EFT_REASON, "like",
				new String[] { eftReason });
	}

	private void getDrillDownList() {
		deptList = getDrillDown("Department", new String[0]);
	}

	public String addTransfer() throws Exception {
		String currentEmpNo = getCurrentEmpNo();
		if (employeeId == null || "".equals(employeeId))
			employeeId = currentEmpNo;
		if ("SUB".equals(super.authorityCondition) && !checkAuth(employeeId)) {
			addErrorInfo("\u60A8\u6CA1\u6709\u65B0\u589E\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "success";
		}
		String fetch[] = { Position.PROP_POSITION_PB_ID };
		IPositionBo positionBo = (IPositionBo) getBean("positionBo");
		Position position = positionBo.getPosById(eftNewPosition, fetch);
		if (eftTransferType.equals("4")
				&& (eftNewPosition == null || eftNewPosition.equals("")))
			position = positionBo.getPosByEmpNo(employeeId, fetch);
		Emptransfer emptransfer = new Emptransfer();
		emptransfer.setEftTransferDate(eftTransferDate);
		emptransfer.setEftTransferType(eftTransferType);
		emptransfer.setEftReason(eftReason);
		emptransfer.setEftComments(eftComments);
		emptransfer.setEmployee(new Employee(employeeId));
		emptransfer.setEftOldDeptNo(new Department(eftOldDeptNo));
		emptransfer.setEftNewDeptNo(new Department(eftNewDeptNo));
		emptransfer.setEftNewEmpType(new Emptype(eftNewEmpType));
		emptransfer.setEftCreateBy(currentEmpNo);
		emptransfer.setEftCreateDate(new Date());
		emptransfer.setEftLastChangeBy(currentEmpNo);
		emptransfer.setEftLastChangeTime(new Date());
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		List result = empBo.batchTransfer(emptransfer,
				new String[] { employeeId }, currentEmpNo, position);
		if (result != null && result.size() > 0) {
			addActionError((String) result.get(0));
			return "success";
		} else {
			addActionMessage("\u5458\u5DE5\u53D8\u52A8\u8BB0\u5F55\u65B0\u589E\u6210\u529F\u3002");
			return "success";
		}
	}

	public String deleteTransfer(String etfId) {
		String auth = DWRUtil.checkAuth("EmpTransfer", "deleteTransfer");
		if ("error".equalsIgnoreCase(auth))
			return "noauth";
		IEmpTransferBo empConstractBo = (IEmpTransferBo) getBean("empTransferBo");
		Emptransfer etf = empConstractBo.getById(etfId);
		if (etf == null)
			return "noId";
		if ("SUB".equals(auth) && !checkAuth(etf.getEmployee().getId())) {
			return "noauth";
		} else {
			empConstractBo.delete(etfId, getCurrentEmp());
			return "success";
		}
	}

	public String updateTransfer() throws Exception {
		String currentEmpNo = getCurrentEmpNo();
		if (updateEtfId == null || "".equals(updateEtfId))
			return "noId";
		if ("SUB".equals(super.authorityCondition) && !checkAuth(employeeId)) {
			addErrorInfo("\u60A8\u6CA1\u6709\u4FEE\u6539\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "success";
		} else {
			Emptransfer emptransfer = new Emptransfer();
			emptransfer.setEftTransferDate(eftTransferDate);
			emptransfer.setEftTransferType(eftTransferType);
			emptransfer.setEftReason(eftReason);
			emptransfer.setEftComments(eftComments);
			emptransfer.setEmployee(new Employee(employeeId));
			emptransfer.setEftNewDeptNo(new Department(eftNewDeptNo));
			emptransfer.setEftLastChangeBy(currentEmpNo);
			emptransfer.setEftLastChangeTime(new Date());
			IEmpTransferBo empTransferBo = (IEmpTransferBo) getBean("empTransferBo");
			empTransferBo.update(emptransfer, updateEtfId);
			clear();
			addActionMessage("\u5458\u5DE5\u53D8\u52A8\u4FEE\u6539\u6210\u529F\u3002");
			return "success";
		}
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	public String getEftId() {
		return eftId;
	}

	public void setEftId(String eftId) {
		this.eftId = eftId;
	}

	public Date getEftTransferDate() {
		return eftTransferDate;
	}

	public void setEftTransferDate(Date eftTransferDate) {
		this.eftTransferDate = eftTransferDate;
	}

	public String getEftReason() {
		return eftReason;
	}

	public void setEftReason(String eftReason) {
		this.eftReason = eftReason;
	}

	public String getEftComments() {
		return eftComments;
	}

	public void setEftComments(String eftComments) {
		this.eftComments = eftComments;
	}

	public String getUpdateEtfId() {
		return updateEtfId;
	}

	public void setUpdateEtfId(String updateEtfId) {
		this.updateEtfId = updateEtfId;
	}

	public String getEftOldDeptNo() {
		return eftOldDeptNo;
	}

	public void setEftOldDeptNo(String eftOldDeptNo) {
		this.eftOldDeptNo = eftOldDeptNo;
	}

	public String getEftNewDeptNo() {
		return eftNewDeptNo;
	}

	public void setEftNewDeptNo(String eftNewDeptNo) {
		this.eftNewDeptNo = eftNewDeptNo;
	}

	public String getEftTransferType() {
		return eftTransferType;
	}

	public void setEftTransferType(String eftTransferType) {
		this.eftTransferType = eftTransferType;
	}

	public List getDeptList() {
		return deptList;
	}

	public void setDeptList(List deptList) {
		this.deptList = deptList;
	}

	public List getTransferList() {
		return transferList;
	}

	public void setTransferList(List transferList) {
		this.transferList = transferList;
	}

	public Date getEftTransferDateTo() {
		return eftTransferDateTo;
	}

	public void setEftTransferDateTo(Date eftTransferDateTo) {
		this.eftTransferDateTo = eftTransferDateTo;
	}

	private void clear() {
		eftId = null;
		eftOldDeptNo = null;
		eftNewDeptNo = null;
		eftTransferDate = null;
		eftTransferType = null;
		eftTransferDate = null;
		eftReason = null;
		eftTransferDateTo = null;
		employeeId = null;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	public String getOutputIoName() {
		return outputIoName;
	}

	public void setOutputIoName(String outputIoName) {
		this.outputIoName = outputIoName;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public String getEftNewPosition() {
		return eftNewPosition;
	}

	public void setEftNewPosition(String eftNewPosition) {
		this.eftNewPosition = eftNewPosition;
	}

	public String getEftOldDeptName() {
		return eftOldDeptName;
	}

	public void setEftOldDeptName(String eftOldDeptName) {
		this.eftOldDeptName = eftOldDeptName;
	}

	public String getEftNewDeptName() {
		return eftNewDeptName;
	}

	public void setEftNewDeptName(String eftNewDeptName) {
		this.eftNewDeptName = eftNewDeptName;
	}

	public String getEftNewEmpType() {
		return eftNewEmpType;
	}

	public void setEftNewEmpType(String eftNewEmpType) {
		this.eftNewEmpType = eftNewEmpType;
	}

	private static final long serialVersionUID = 1L;
	private Emptransfer empTransfer;
	private String eftId;
	private String employeeId;
	private Date eftTransferDate;
	private Date eftTransferDateTo;
	private String eftTransferType;
	private String eftNewEmpType;
	private String eftReason;
	private String eftComments;
	private String updateEtfId;
	private String eftOldDeptNo;
	private String eftNewDeptNo;
	private String eftOldDeptName;
	private String eftNewDeptName;
	private List deptList;
	private List transferList;
	private String eftNewPosition;
	private Pager page;
	private String outmatchModelId;
	private String outputIoName;
	private String searchOrExport;
}
